package com.qf.service.impl;

import com.qf.entity.Order;
import com.qf.entity.OrderExample;
import com.qf.entity.User;
import com.qf.mapper.OrderMapper;
import com.qf.mapper.UserMapper;
import com.qf.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description TODO
 * @Author 刘珈玮
 * @Date 2020/7/25 16:54
 */
@Service
public class PayServiceImpl implements PayService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    @Transactional
    public boolean payInfo(Integer id, Float balance) {
        if (id != null && balance != null) {
            User user = userMapper.selectByPrimaryKey(id);
            if (user != null) {
                Float balance1 = user.getBalance();
                Float newBalance = balance1 - balance;
                if (newBalance >= 0) {
                    user.setBalance(newBalance);
                    int i = userMapper.updateByPrimaryKeySelective(user);
                    if (i == 1) {
                        Order order = new Order();
                        OrderExample orderExample = new OrderExample();
                        OrderExample.Criteria criteria = orderExample.createCriteria();
                        criteria.andUserIdEqualTo(id);
                        List<Order> orders = orderMapper.selectByExample(orderExample);
                        for (Order order1 : orders) {
                            Integer ordersId = order1.getOrdersId();
                            if (ordersId != null) {
                                order1.setOrdersstatus("已付款");
                                int i1 = orderMapper.updateByPrimaryKeySelective(order1);
                                if (i1 == 1) {
                                    continue;
                                } else {
                                    return false;
                                }
                            } else {
                                return false;
                            }
                        }
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
